//
//  TTSWidget.m
//  easyTaxi
//
//  Created by ellenpage on 12-12-14.
//  Copyright (c) 2012年 ellenpage. All rights reserved.
//

#import "Constant.h"
#import "TTSWidget.h"
#import "PassValueClass.h"

@interface TTSWidget () <IFlySynthesizerControlDelegate,IFlyRecognizeControlDelegate>

@end

@implementation TTSWidget

//kSingleton(TTSWidget)

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self)
    {
        self = [[[NSBundle mainBundle] loadNibNamed:@"TTSWidget" owner:self options:nil] objectAtIndex:0];
    }
    return self;
}

- (id)init
{
    if (self = [super init])
    {
    }
    return self;
}


- (void)createSynthesizerWidgetWithoutView
{
    NSString *abc = [[NSString alloc] initWithFormat:@"server_url=%@,appid=%@",kXunFeiUrl,kXunFeiKey];
    if (!_synTheSizeControl)
    {
        _synTheSizeControl = [[IFlySynthesizerControl alloc] initWithOrigin:kWidgetOrigin initParam:abc];
        _synTheSizeControl.delegate = self;
    }
}

- (void)createSynthesizerWidget
{
    NSString *abc = [[NSString alloc] initWithFormat:@"server_url=%@,appid=%@",kXunFeiUrl,kXunFeiKey];
    if (!_synTheSizeControl)
    {
        _synTheSizeControl = [[IFlySynthesizerControl alloc] initWithOrigin:kWidgetOrigin initParam:abc];
        _synTheSizeControl.delegate = self;
        [self addSubview:_synTheSizeControl];
    }
}

- (void)createRecognizerWidget
{
    NSString *abc = [[NSString alloc] initWithFormat:@"server_url=%@,appid=%@",kXunFeiUrl,kXunFeiKey];
    _recognizeControl = [[IFlyRecognizeControl alloc] initWithOrigin:kWidgetOrigin initParam:abc];
    [_recognizeControl setEngine:@"sms" engineParam:nil grammarID:nil];
    [_recognizeControl setSampleRate:16000];
    _recognizeControl.delegate = self;
    [self addSubview:_recognizeControl];
    NSLog(@"%f--%f",_recognizeControl.frame.size.height,_recognizeControl.frame.size.width);
}

- (void)synthesizeVoice:(NSString *)string//文字转语音
{
    [_synTheSizeControl setText:string params:nil];
    [_synTheSizeControl start];
}

- (void)startRecongnize
{
    [_recognizeControl start];
}

- (void)onRecognizeEnd:(IFlyRecognizeControl *)iFlyRecognizeControl theError:(SpeechError) error
{
    [self removeFromSuperview];
}

- (void)onResult:(IFlyRecognizeControl *)iFlyRecognizeControl theResult:(NSArray *)resultArray
{
    NSLog(@"%@",resultArray);
    NSString *voiceStr = [[resultArray objectAtIndex:0] objectForKey:@"NAME"];
    if (voiceStr != nil)
    {
        NSString *newStr = [voiceStr substringToIndex:[voiceStr length] - 1];

        [PassValueClass sharedIntance].voiceStr = newStr;
        if ([self.delegate performSelector:@selector(getAddress1)] && [PassValueClass sharedIntance].voiceNumber == 0)
        {
            [self.delegate getAddress1];
        }
    }
}

@end
